/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cryptpass;

import java.util.Properties;
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import com.thejaswi.utilities.*;

/**
 *
 * @author BhatG
 */
public class Main {
   static Properties config=new Properties();
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       // load configurations: OS, java-system, command-line arguments and config file
       Configurator cfg=new Configurator(args);
       cfg.loadFile(cfg.getProperty("configFile"));
       //re-apply command line arguments to supersede config file values
       cfg.load(args);
       config=cfg.getProperties();
       String sourceFilePath=config.getProperty("sourcefile");
       String targetFilePath=config.getProperty("targetfile");
       String fieldDelimiter=config.getProperty("fieldDelimiter");

       System.out.println("------- encrypting file ------------------------");
       encryptfile(sourceFilePath, targetFilePath, fieldDelimiter);
       System.out.println("------- decrypting file ------------------------");
       decryptfile(targetFilePath, fieldDelimiter);
       System.out.println("-------- completed -----------------------");
    }

    public static void decryptfile(String sourceFilePath, String fieldDelimiter) {
      try {
         Cryptographer crypt = new Cryptographer();
         String inLine = "";
         File inputFile = new File(sourceFilePath);

         if(! inputFile.exists()) {
            System.out.println("Error: source-file "+sourceFilePath+" not found!");
         }
         else {
            BufferedReader buffread = new BufferedReader(new FileReader(sourceFilePath));

            while ( (inLine = buffread.readLine()) != null ) {
               String[] fields = inLine.split(fieldDelimiter);
               String keyString=fields[0];
               String userName=crypt.decrypt(fields[1],keyString);
               String serverName=crypt.decrypt(fields[2],keyString);
               String password=crypt.decrypt(fields[3],keyString);

               String outLine = keyString+fieldDelimiter+
                                userName+fieldDelimiter+
                                serverName+fieldDelimiter+
                                password;
               System.out.println("outLine="+outLine);
            }
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void encryptfile(String sourceFilePath, String targetFilePath, String fieldDelimiter) {
      try {
         fieldDelimiter = (fieldDelimiter==null ? ":" : fieldDelimiter );
         Cryptographer crypt = new Cryptographer();

         String inLine = "";
         BufferedReader buffread = new BufferedReader(new FileReader(sourceFilePath));

         File outputFile = new File(targetFilePath);
         //if file doesnt exists, then create it
         if(!outputFile.exists()){
            outputFile.createNewFile();
         }
         FileWriter fileWriter = new FileWriter(outputFile.getName());
         BufferedWriter buffWrite = new BufferedWriter(fileWriter);

         while ( (inLine = buffread.readLine()) != null ) {
            String keyString=crypt.generateKeyHexString();
            String[] fields = inLine.split(fieldDelimiter);
            String userName=crypt.encrypt(fields[0],keyString);
            String serverName=crypt.encrypt(fields[1],keyString);
            String password=crypt.encrypt(fields[2],keyString);
            String outLine = keyString+":"+userName+":"+serverName+":"+password;
            buffWrite.write(outLine);
System.out.println("inLine="+inLine);
System.out.println("outLine="+outLine);
System.out.println("");
         }
         buffWrite.close();
         fileWriter.close();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
} // end of class Main
